package me.kevin.modules.mockexam.service.exam2023.impl;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import me.kevin.modules.mockexam.pojo.MeQuestion;
import me.kevin.modules.mockexam.service.MeQuestionService;
import me.kevin.modules.mockexam.service.exam2023.ExamService;
import me.kevin.modules.mockexam.service.exam2023.domain.BaseResponse;
import me.kevin.modules.mockexam.service.exam2023.domain.ExamItem;
import me.kevin.modules.mockexam.service.exam2023.domain.GetExamReq;
import me.kevin.modules.mockexam.utils.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: me.kevin.modules.mockexam.service.exam2023.impl
 * @description: 考题导出
 * @author: lilf@bwoil.com
 * @create: 2024-09-25 10:30
 **/
@Service
@Slf4j
public class ExamServiceImpl implements ExamService {

    @Autowired
    private MeQuestionService meQuestionService;

    final String getExamUrl = "http://exam.ln263.com/exam2023/Exam/fzks.html?act=getexam";
    @Override
    public void crawling168Data() {
        log.info("开始爬取考题数据");
        Map<String,String> headers = new HashMap<>();
        headers.put("Cookie","PHPSESSID=5bde0c6fdc0432b9552249d0701653c4");
        headers.put("Origin","http://exam.ln263.com");

        GetExamReq getExamReq = new GetExamReq();
        getExamReq.setKey("");
        getExamReq.setUuid("");
        ExamItem  examItem =  getExamData(getExamReq, headers);

        if(examItem == null){
            log.info("examItem is null");
        }


    }


    /**
     * 获取考题数据
     * @param getExamReq
     * @param headers
     * @return
     */
    private ExamItem getExamData(GetExamReq getExamReq, Map<String,String> headers) {
        try {
            ExamItem examItem = null;
            String response =   HttpUtil.sendPostForm(getExamUrl, getExamReq, headers);
            BaseResponse baseResponse = JSONUtil.toBean(response, BaseResponse.class);
            if (baseResponse.getCode().equals("200")) {
                log.info("获取考题数据成功");
                examItem = JSONUtil.toBean(baseResponse.getData().toString(), ExamItem.class);
            }else {
                log.info("获取考题数据失败");
            }
            return examItem;
        }catch (Exception e) {
            log.error("获取考题数据失败-getExamReq:{}",getExamReq);
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 保存考题数据
     * @param examItem
     */
    private void saveExamItem(ExamItem examItem){

        MeQuestion meQuestion = new MeQuestion();
        meQuestion.setId(examItem.getSTUUID());


        Object result = meQuestionService.addQuestion(meQuestion);

    }


}
